<template>
  <el-card>
    <el-table border :data="skuList" v-loading="loading">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80"
      ></el-table-column>
      <el-table-column label="名称" prop="skuName"></el-table-column>
      <el-table-column label="描述" prop="skuDesc"></el-table-column>
      <el-table-column label="默认图片">
        <template v-slot="{ row, $index }">
          <el-image :src="row.skuDefaultImg" style="width: 100px"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="重量(KG)" prop="weight"></el-table-column>
      <el-table-column label="价格(元)" prop="price"></el-table-column>
      <el-table-column label="操作" width="260px" align="center">
        <template v-slot="{ row }">
          <el-tooltip
            class="box-item"
            effect="dark"
            content="下架"
            placement="top"
          >
            <el-button
              size="small"
              v-show="row.isSale === 0"
              type="success"
              :icon="Bottom"
              @click="toCancelSale(row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="box-item"
            effect="dark"
            content="上架"
            placement="top"
          >
            <el-button
              size="small"
              v-show="row.isSale !== 0"
              type="info"
              :icon="Top"
              @click="toOnSale(row)"
            ></el-button>
          </el-tooltip>

          <el-button size="small" type="primary" :icon="Edit"></el-button>
          <el-tooltip
            class="box-item"
            effect="dark"
            content="查看"
            placement="top"
          >
            <el-button
              size="small"
              type="info"
              :icon="InfoFilled"
              @click="toCheckSkuInfo(row)"
            ></el-button>
          </el-tooltip>
          <el-popconfirm
            width="220"
            confirm-button-text="确定"
            cancel-button-text="No, Thanks"
            :icon="InfoFilled"
            icon-color="#626AEF"
            title="您真的要删除当前Sku数据么?"
            @confirm="toDeleteSkuInfo(row, $index)"
            @cancel="cancelEvent"
          >
            <template #reference>
              <el-button size="small" type="danger" :icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout=" prev, pager, next, jumper, ->,sizes,total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 查看skuInfo数据 使用抽屉的效果 -->
    <el-drawer
      class="skuInfo"
      size="50%"
      v-model="drawerVisible"
      direction="rtl"
      :with-header="false"
    >
      <el-row :gutter="20">
        <el-col :span="6">
          <h4>名称</h4>
        </el-col>
        <el-col :span="18">
          <span>{{ skuInfo.skuName }}</span>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <h4>描述</h4>
        </el-col>
        <el-col :span="18">
          <span>{{ skuInfo.skuDesc }}</span>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <h4>价格</h4>
        </el-col>
        <el-col :span="18">
          <span>{{ skuInfo.price }}.00</span>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <h4>平台属性</h4>
        </el-col>
        <el-col :span="18">
          <el-tag
            class="ml-2"
            style="margin-right: 4px"
            v-for="attrValue in skuInfo.skuAttrValueList"
            type="success"
            :key="attrValue.id"
            >{{ attrValue.valueName }}</el-tag
          >
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <h4>销售属性</h4>
        </el-col>
        <el-col :span="18">
          <el-tag
            class="ml-2"
            style="margin-right: 4px"
            v-for="saleAttrValue in skuInfo.skuSaleAttrValueList"
            type="success"
            :key="saleAttrValue.id"
            >{{ saleAttrValue.saleAttrValueName }}</el-tag
          >
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <h4>商品图片</h4>
        </el-col>
        <el-col :span="18">
          <el-carousel :interval="2000">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <el-image :src="item.imgUrl"></el-image>
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </el-card>
</template>
<script lang="ts">
export default {
  name: 'Sku',
};
</script>

<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import {
  Plus,
  Edit,
  Delete,
  InfoFilled,
  Top,
  Bottom,
} from '@element-plus/icons-vue';
import {
  reqDeleteSkuInfo,
  reqGetAllSkuListData,
  reqGetSkuInfoData,
  reqSkuCancelSale,
  reqSkuOnSale,
} from '@/api/product/sku';
import { ElMessage } from 'element-plus';

const currentPage = ref(1);
const pageSize = ref(10);
const skuList = ref([]);
const total = ref(0);
const small = ref(false);
const loading = ref(true);
const background = ref(false);
const disabled = ref(false);

const drawerVisible = ref(false);
const skuInfo = ref({});

// 1. 定义一个获取所有的skuList数据的函数
const getAllSkuListData = async () => {
  loading.value = true;
  const result = await reqGetAllSkuListData(currentPage.value, pageSize.value);
  loading.value = false;
  skuList.value = result.records;
  total.value = result.total;
};

// 2. 页面组件挂载完毕之后就要调用方法 获取所有的skuList数据
onMounted(() => {
  getAllSkuListData();
});

// 3. 单击页码实现分页效果
const handleCurrentChange = (val: number) => {
  currentPage.value = val;
  getAllSkuListData();
};

// 4. 修改每页显示的数量
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  currentPage.value = 1; // 默认跳转到第1页
  getAllSkuListData();
};

// 5. 下架商品
const toCancelSale = async (row) => {
  await reqSkuCancelSale(row.id);
  ElMessage.success('商品下架成功');

  // 原来的上架状态 isSale是0  下架状态 isSale是1
  row.isSale = 1 - row.isSale;
};

// 6. 上架商品
const toOnSale = async (row) => {
  await reqSkuOnSale(row.id);
  ElMessage.success('商品上架成功');
  row.isSale = 1 - row.isSale;
};

// 7. 删除skuInfo商品
const toDeleteSkuInfo = async (row, index) => {
  await reqDeleteSkuInfo(row.id);
  ElMessage.success('删除sku成功');

  // 使用剩下的数据重新渲染页面
  skuList.value.splice(index, 1);
};

// 8. 取消删除
const cancelEvent = () => {
  ElMessage.info('取消了删除');
};

// 9. 查看一条skuInfo数据
const toCheckSkuInfo = async (row) => {
  const result = await reqGetSkuInfoData(row.id);

  skuInfo.value = result;
  // 弹出抽屉
  drawerVisible.value = true;
};
</script>

<style lang="scss" scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 300px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

.el-drawer__body h4 {
  margin: 0;
  padding: 0;
  height: 40px;
  text-align: right;
}
.el-drawer__body {
  .el-carousel__container {
    height: 400px;
    .el-image {
      .el-image__inner {
        height: 90%;
      }
    }
  }
}
</style>
